<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>通用方法 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    
    <link rel="prev" href="./chapter04.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="5"
        data-chapter-title="通用方法"
        data-filepath="chapter05.md"
        data-basepath="."
        data-revision="Sun Feb 09 2020 19:04:23 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        元组的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02/index.html">
            
                
                    <a href="./chapter02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        字典的基本使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter02/section01.html">
            
                
                    <a href="./chapter02/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        字典的增删改查
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter02/section02.html">
            
                
                    <a href="./chapter02/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        字典的遍历
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        集合的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        转换相关
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        通用方法
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <p>&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5217;&#x8868;&#x3001;&#x5143;&#x7EC4;&#x3001;&#x5B57;&#x5178;&#x548C;&#x96C6;&#x5408;&#xFF0C;&#x5B83;&#x4EEC;&#x6709;&#x5F88;&#x591A;&#x76F8;&#x540C;&#x70B9;&#xFF0C;&#x90FD;&#x662F;&#x7531;&#x591A;&#x4E2A;&#x5143;&#x7D20;&#x7EC4;&#x5408;&#x6210;&#x7684;&#x4E00;&#x4E2A;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x4EEC;&#x90FD;&#x6709;&#x4E00;&#x4E9B;&#x53EF;&#x4EE5;&#x5171;&#x540C;&#x4F7F;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<h2 id="&#x7B97;&#x6570;&#x8FD0;&#x7B97;&#x7B26;">&#x7B97;&#x6570;&#x8FD0;&#x7B97;&#x7B26;</h2>
<p>&#x5728;Python&#x91CC;&#xFF0C;&#x5E38;&#x89C1;&#x7684;&#x7B97;&#x6570;&#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x6709;&#x4E00;&#x4E9B;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E8E;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x4EEC;&#x6267;&#x884C;&#x7684;&#x7ED3;&#x679C;&#x4E5F;&#x7A0D;&#x6709;&#x533A;&#x522B;&#x3002;</p>
<table>
<thead>
<tr>
<th style="text-align:left">&#x8FD0;&#x7B97;&#x7B26;</th>
<th>Python &#x8868;&#x8FBE;&#x5F0F;</th>
<th>&#x7ED3;&#x679C;</th>
<th>&#x63CF;&#x8FF0;</th>
<th>&#x652F;&#x6301;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">+</td>
<td>[1, 2] + [3, 4]</td>
<td>[1, 2, 3, 4]</td>
<td>&#x5408;&#x5E76;</td>
<td>&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5217;&#x8868;&#x3001;&#x5143;&#x7EC4;</td>
</tr>
<tr>
<td style="text-align:left">-</td>
<td>{1,2,3,4} - {2,3}</td>
<td>{1,4}</td>
<td>&#x96C6;&#x5408;&#x6C42;&#x5DEE;&#x96C6;</td>
<td>&#x96C6;&#x5408;</td>
</tr>
<tr>
<td style="text-align:left">*</td>
<td>[&apos;Hi!&apos;] * 4</td>
<td>[&apos;Hi!&apos;, &apos;Hi!&apos;, &apos;Hi!&apos;, &apos;Hi!&apos;]</td>
<td>&#x590D;&#x5236;</td>
<td>&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5217;&#x8868;&#x3001;&#x5143;&#x7EC4;</td>
</tr>
<tr>
<td style="text-align:left">in</td>
<td>3 in (1, 2, 3)</td>
<td>True</td>
<td>&#x5143;&#x7D20;&#x662F;&#x5426;&#x5B58;&#x5728;</td>
<td>&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5217;&#x8868;&#x3001;&#x5143;&#x7EC4;&#x3001;&#x5B57;&#x5178;</td>
</tr>
<tr>
<td style="text-align:left">not in</td>
<td>4 not in (1, 2, 3)</td>
<td>True</td>
<td>&#x5143;&#x7D20;&#x662F;&#x5426;&#x4E0D;&#x5B58;&#x5728;</td>
<td>&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5217;&#x8868;&#x3001;&#x5143;&#x7EC4;&#x3001;&#x5B57;&#x5178;</td>
</tr>
</tbody>
</table>
<h3 id="">+</h3>
<p>&#x52A0;&#x6CD5;&#x8FD0;&#x7B97;&#x7B26;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5217;&#x8868;&#x548C;&#x5143;&#x7EC4;&#xFF0C;&#x7528;&#x6765;&#x62FC;&#x63A5;&#x591A;&#x4E2A;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#xFF0C;&#x4E0D;&#x80FD;&#x7528;&#x4E8E;&#x5B57;&#x5178;&#x548C;&#x96C6;&#x5408;(&#x601D;&#x8003;&#xFF1A;&#x4E3A;&#x4EC0;&#x4E48;&#x5B57;&#x5178;&#x548C;&#x96C6;&#x5408;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;)&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-string">&quot;hello &quot;</span> + <span class="hljs-string">&quot;world&quot;</span>
<span class="hljs-string">&apos;hello world&apos;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>] + [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]
<span class="hljs-prompt">&gt;&gt;&gt; </span>(<span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-string">&apos;b&apos;</span>) + (<span class="hljs-string">&apos;c&apos;</span>, <span class="hljs-string">&apos;d&apos;</span>)
(<span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-string">&apos;c&apos;</span>, <span class="hljs-string">&apos;d&apos;</span>)
</code></pre>
<h3 id="">-</h3>
<p>&#x51CF;&#x6CD5;&#x53EA;&#x80FD;&#x7528;&#x4E8E;&#x96C6;&#x5408;&#x91CC;&#xFF0C;&#x7528;&#x6765;&#x6C42;&#x4E24;&#x4E2A;&#x96C6;&#x5408;&#x7684;&#x5DEE;&#x96C6;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>{<span class="hljs-number">1</span>, <span class="hljs-number">6</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>, <span class="hljs-number">12</span>, <span class="hljs-number">3</span>} - {<span class="hljs-number">4</span>, <span class="hljs-number">8</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>}
{<span class="hljs-number">9</span>, <span class="hljs-number">10</span>, <span class="hljs-number">12</span>, <span class="hljs-number">6</span>}
</code></pre>
<h3 id="">*</h3>
<p>&#x52A0;&#x6CD5;&#x8FD0;&#x7B97;&#x7B26;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5217;&#x8868;&#x548C;&#x5143;&#x7EC4;&#xFF0C;&#x7528;&#x6765;&#x5C06;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x91CD;&#x590D;&#x591A;&#x6B21;&#xFF0C;&#x540C;&#x6837;&#x4E0D;&#x80FD;&#x7528;&#x4E8E;&#x5B57;&#x5178;&#x548C;&#x96C6;&#x5408;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-string">&apos;ab&apos;</span> * <span class="hljs-number">4</span>
<span class="hljs-string">&apos;ababab&apos;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>] * <span class="hljs-number">4</span>
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>]
<span class="hljs-prompt">&gt;&gt;&gt; </span>(<span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-string">&apos;b&apos;</span>) * <span class="hljs-number">4</span>
(<span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-string">&apos;b&apos;</span>)
</code></pre>
<h3 id="in">in</h3>
<p>in&#x548C;not in&#x6210;&#x5458;&#x8FD0;&#x7B97;&#x7B26;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x6240;&#x6709;&#x7684;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x3002;&#x4F46;&#x662F;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;in &#x548C; not in &#x5728;&#x5BF9;&#x5B57;&#x5178;&#x8FDB;&#x884C;&#x5224;&#x65AD;&#x65F6;&#xFF0C;&#x662F;&#x67E5;&#x770B;&#x6307;&#x5B9A;&#x7684;key&#x662F;&#x5426;&#x5B58;&#x5728;&#xFF0C;&#x800C;&#x4E0D;&#x662F;value.</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-string">&apos;llo&apos;</span> <span class="hljs-keyword">in</span> <span class="hljs-string">&apos;hello world&apos;</span>
<span class="hljs-keyword">True</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-number">3</span> <span class="hljs-keyword">in</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>]
<span class="hljs-keyword">False</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-number">4</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
<span class="hljs-keyword">True</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-string">&quot;name&quot;</span> <span class="hljs-keyword">in</span> {<span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;chris&quot;</span>, <span class="hljs-string">&quot;age&quot;</span>:<span class="hljs-number">18</span>}
<span class="hljs-keyword">True</span>
</code></pre>
<h2 id="&#x904D;&#x5386;">&#x904D;&#x5386;</h2>
<p>&#x901A;&#x8FC7;for ... in ... &#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x904D;&#x5386;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5217;&#x8868;&#x3001;&#x5143;&#x7EC4;&#x3001;&#x5B57;&#x5178;&#x3001;&#x96C6;&#x5408;&#x7B49;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x3002;</p>
<h3 id="&#x5B57;&#x7B26;&#x4E32;&#x904D;&#x5386;">&#x5B57;&#x7B26;&#x4E32;&#x904D;&#x5386;</h3>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>a_str = <span class="hljs-string">&quot;hello world&quot;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> char <span class="hljs-keyword">in</span> a_str:
<span class="hljs-prompt">... </span>    print(char,end=<span class="hljs-string">&apos; &apos;</span>)
...
h e l l o   w o r l d
</code></pre>
<h3 id="&#x5217;&#x8868;&#x904D;&#x5386;">&#x5217;&#x8868;&#x904D;&#x5386;</h3>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>a_list = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> a_list:
<span class="hljs-prompt">... </span>    print(num,end=<span class="hljs-string">&apos; &apos;</span>)
...
<span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span>
</code></pre>
<h3 id="&#x5143;&#x7EC4;&#x904D;&#x5386;">&#x5143;&#x7EC4;&#x904D;&#x5386;</h3>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>a_turple = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>)
<span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> a_turple:
<span class="hljs-prompt">... </span>    print(num,end=<span class="hljs-string">&quot; &quot;</span>)
<span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span>
</code></pre>
<h2 id="&#x5E26;&#x4E0B;&#x6807;&#x7684;&#x904D;&#x5386;">&#x5E26;&#x4E0B;&#x6807;&#x7684;&#x904D;&#x5386;</h2>
<p>&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x90FD;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; enumerate &#x5185;&#x7F6E;&#x7C7B;&#x8FDB;&#x884C;&#x5305;&#x88C5;&#x6210;&#x4E00;&#x4E2A; enumerate &#x5BF9;&#x8C61;&#x3002;&#x5BF9;enumerate&#x8FDB;&#x884C;&#x904D;&#x5386;&#xFF0C;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x7684;&#x4E0B;&#x6807;&#x548C;&#x5143;&#x7D20;&#x3002;</p>
<pre><code class="lang-python">nums = [<span class="hljs-number">12</span>, <span class="hljs-number">9</span>, <span class="hljs-number">8</span>, <span class="hljs-number">5</span>, <span class="hljs-number">4</span>, <span class="hljs-number">7</span>, <span class="hljs-number">3</span>, <span class="hljs-number">6</span>]

<span class="hljs-comment"># &#x5C06;&#x5217;&#x8868; nums &#x5305;&#x88C5;&#x6210; enumerate &#x5BF9;&#x8C61;</span>
<span class="hljs-keyword">for</span> i, num <span class="hljs-keyword">in</span> enumerate(nums): <span class="hljs-comment"># i&#x8868;&#x793A;&#x5143;&#x7D20;&#x4E0B;&#x6807;&#xFF0C;num&#x8868;&#x793A;&#x5217;&#x8868;&#x91CC;&#x7684;&#x5143;&#x7D20;</span>
    print(<span class="hljs-string">&apos;&#x7B2C;%d&#x4E2A;&#x5143;&#x7D20;&#x662F;%d&apos;</span> % (i, num))
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter04.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 转换相关"><i class="fa fa-angle-left"></i></a>
        
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
